package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.FolderActivity;
import flc.ast.activity.LocalFileActivity;
import flc.ast.adapter.FolderAdapter;
import flc.ast.databinding.FragmentPlayerBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PlayerFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PlayerFragment extends BaseNoModelFragment<FragmentPlayerBinding> {

    private FolderAdapter folderAdapter;
    private List<String> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_player;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        
        mDataBinding.ivAdd.setOnClickListener(this);

        mDataBinding.rvFolderList.setLayoutManager(new LinearLayoutManager(mContext));
        folderAdapter = new FolderAdapter(mContext);
        mDataBinding.rvFolderList.setAdapter(folderAdapter);
        folderAdapter.setOnItemClickListener(new FolderAdapter.OnItemClickListener() {
            @Override
            public void onClick(String path, int position) {
                FolderActivity.basePath = path;
                startActivity(FolderActivity.class);
            }

            @Override
            public void onClickDelete(String path, int position) {
                FileUtils.delete(path);
                ToastUtils.showShort(getString(R.string.delete_duc));
                getData();
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAdd://添加
                getManagePermission();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myFolder;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        if (listShow.size() > 0) {
            folderAdapter.setmDatas(listShow);
            folderAdapter.notifyDataSetChanged();
            mDataBinding.rvFolderList.setVisibility(View.VISIBLE);
            mDataBinding.ivFolderNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvFolderList.setVisibility(View.GONE);
            mDataBinding.ivFolderNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getManagePermission() {
        StkPermissionHelper.reqManageExternalStoragePermission().reqPermissionDesc(getString(R.string.get_manage_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                startActivity(LocalFileActivity.class);
            }
        }).request();
    }

}